var express = require('express');
var router = express.Router();
var EventProxy = require('eventproxy');
var util = require('util');

var logger = require("../models/logger");
var Jobs = require("../models/Jobs");
var resultJson = require("../result-json");
var Utils = require("../utils");
var Consts = require("../consts");
var User = require("../models/User");

router.post('/', function(req, res) {
    res.send('respond with a resource');
});


router.post('/upsert', function(req, res) {
    var job = req.body;
    job.create = new Date();
    var _id = job._id;
    if (job._id) {
        delete job._id;
        Jobs.update({
            _id:_id
        }, {
            $set: job
        }).exec(function(err) {
            if (err) {
                logger.error(err);
                return res.send(resultJson(2));
            }
            res.send(resultJson(0));
        });
    } else {
        new Jobs(job).save(function(err, job) {
            if (err) {
                logger.error(err);
                return res.send(resultJson(2));
            }
            res.send(resultJson(0, job));
        });
    }
});

router.post('/bj', function(req, res) {
    list(req, res);
});
router.post('/sh', function(req, res) {
    list(req, res);
});
router.post('/gz', function(req, res) {
    list(req, res);
});
router.post('/sz', function(req, res) {
    list(req, res);
});
router.post('/qt', function(req, res) {
    list(req, res);
});

function list(req, res) {
    var job = req.body;
    var option = {};
    if(job.place)
      option.place = job.place;
    Jobs.find(option).sort({
        create: -1
    }).exec(function(err, job) {
        if (err) {
            logger.error(err);
            return res.send(resultJson(2));
        }
        if(!job||job.length==0||job==undefined){
           res.send(resultJson(0, job));   
        }else{
            var obj = new Object();
            var arr = new Array();
            var count = 0;
            for(var i = 0 ;i<job.length;i++){
              obj = job[i];
              User.findOne({
                        _id: job[i].from,//TODO :这里需要添加条件：好友同意之后方可列出status=1
                },function(err,user){
                    console.log("user"+user);
                    if(err){
                      return res.send(resultJson(2));
                    }
                    ++count;
                    if(user){
                        obj.photo = user.photo;
                    }
                    arr.push(obj);
                    if(count == job.length){
                        //res.send(resultJson(0, arr));
                        res.send(resultJson(0, job));
                   }
                });
        } 
        }
    });  
};


router.post('/list', function(req, res) {
    var job = req.body;
    Jobs.find().sort({
        create: -1
    }).exec(function(err, job) {
        if (err) {
            logger.error(err);
            return res.send(resultJson(2));
        }
        res.send(resultJson(0, job));
    });
   
});

//删除
router.post('/remove', function(req, res) {
    var job = req.body;
    Jobs.remove({
        _id: job._id
    }, function(err) {
        if (err) {
            logger.error(err);
            res.send(resultJson(2));
        } else
            res.send(resultJson(0));
    });
});



module.exports = router;